package net.zjitc.dao.product.impl;

import net.zjitc.Utils.C3P0Utils;
import net.zjitc.dao.product.ProductDao;
import net.zjitc.domain.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by hasee on 2017/10/23.
 */
public class ProductDaoImpl implements ProductDao {
    public static final int PAGE_SIZE =20;
    @Override
    public List<Product> findAll() {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        String sql = "select * from product";
        List<Product> list = null;
        try {
            list = (List<Product>) runner.query(sql, new BeanListHandler(Product.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Product findById(String id){
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        String sql = "select * from product where pid = ?";
        Product product = null;
        try {
            product = (Product) runner.query(sql,new BeanHandler(Product.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public void update(Product product) {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        String sql = "update product set pname=?,marketPrice=?,shopPrice=?,pdesc=? where pid=?";
        try {
            runner.update(sql,product.getPname(),product.getMarketPrice(),product.getShopPrice(),product.getPdesc(),product.getPid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeById(String id) {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        String sql = "delete from product where pid = ?";
        try {
            runner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insert(Product product) {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        String sql = "insert into product(pname,marketPrice,shopPrice,pdesc) values (?,?,?,?)";
        try {
            runner.update(sql,product.getPname(),product.getMarketPrice(),product.getShopPrice(),product.getPdesc());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Product> searchByPage(int page){
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        String sql = "select * from product limit ?,?";
        List<Product> list =null;
        try {
            list = (List<Product>) runner.query(sql,new BeanListHandler(Product.class),((page-1)*PAGE_SIZE),PAGE_SIZE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Product> findByKeyword(String keyword) {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataScource());
        keyword = "%"+keyword+"%";
        String sql = "select * from product where pname like ?";
        List<Product> list = null;
        try {
            list = (List<Product>) runner.query(sql,new BeanListHandler(Product.class),keyword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
